package com.pansoft.kline.widget;

import android.content.Context;
import com.google.android.material.tabs.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.efounder.utils.ResStringUtil;
import com.pansoft.kline.adapter.KIndexPopAdapter;
import com.pansoft.kline.model.KIndexPopBean;
import com.pansoft.kline.model.KIndexSelectEvent;
import com.pansoft.openplanet.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_10_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_12_HOUR;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_2_HOUR;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_30_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_3_DAY;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_3_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_4_HOUR;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_5_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_6_HOUR;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_MONTH;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_WEEK;


/**
 * 各K线指标选择弹窗
 * @author will
 */
public class KindexPopupWindow {

    private Context context;
    private View view;
    private View decorView;
    private String currentSelectedIndex;

    public KindexPopupWindow(Context context, View view, View decorView, String index) {
        this.context = context;
        this.view = view;
        this.decorView = decorView;
        this.currentSelectedIndex = index;
        initView();
    }

    private void initView() {
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        View view = LayoutInflater.from(context).inflate(R.layout.chart_k_index_pop,  null, false);
        final GridView gridView = view.findViewById(R.id.gridView);
        final List<KIndexPopBean> kIndexPopBeans = initKIndexData();
        final KIndexPopAdapter kIndexPopAdapter = new KIndexPopAdapter(context, kIndexPopBeans);
        gridView.setAdapter(kIndexPopAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                for (KIndexPopBean kIndexPopBean : kIndexPopBeans) {
                    kIndexPopBean.setSelected(false);
                }
                kIndexPopBeans.get(position).setSelected(true);
                kIndexPopAdapter.notifyDataSetChanged();
                EventBus.getDefault().post(new KIndexSelectEvent(kIndexPopBeans.get(position).getIndex(),
                        kIndexPopBeans.get(position).getName()));
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(view);
        popupWindow.showAsDropDown(decorView);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setKIndexTabSelected(context, (TabLayout) decorView);
            }
        });
    }

    private List<KIndexPopBean> initKIndexData() {
        List<KIndexPopBean> list = new ArrayList<>();
        KIndexPopBean kIndexPopBean1 = new KIndexPopBean(false, ResStringUtil.getString(R.string.minute_k), K_LINE_TYPE_MINUTE);
        list.add(kIndexPopBean1);
        KIndexPopBean kIndexPopBean2 = new KIndexPopBean(false, ResStringUtil.getString(R.string.minute_k_3), K_LINE_TYPE_3_MINUTE);
        list.add(kIndexPopBean2);
        KIndexPopBean kIndexPopBean3 = new KIndexPopBean(false, ResStringUtil.getString(R.string.minute_k_5), K_LINE_TYPE_5_MINUTE);
        list.add(kIndexPopBean3);
        KIndexPopBean kIndexPopBean4 = new KIndexPopBean(false, ResStringUtil.getString(R.string.minute_k_10), K_LINE_TYPE_10_MINUTE);
        list.add(kIndexPopBean4);
        KIndexPopBean kIndexPopBean5 = new KIndexPopBean(false, ResStringUtil.getString(R.string.minute_k_30), K_LINE_TYPE_30_MINUTE);
        list.add(kIndexPopBean5);
        KIndexPopBean kIndexPopBean6 = new KIndexPopBean(false, ResStringUtil.getString(R.string.hour_k_2), K_LINE_TYPE_2_HOUR);
        list.add(kIndexPopBean6);
        KIndexPopBean kIndexPopBean7 = new KIndexPopBean(false, ResStringUtil.getString(R.string.hour_k_4), K_LINE_TYPE_4_HOUR);
        list.add(kIndexPopBean7);
        KIndexPopBean kIndexPopBean8 = new KIndexPopBean(false, ResStringUtil.getString(R.string.hour_k_6), K_LINE_TYPE_6_HOUR);
        list.add(kIndexPopBean8);
        KIndexPopBean kIndexPopBean9 = new KIndexPopBean(false, ResStringUtil.getString(R.string.hour_k_12), K_LINE_TYPE_12_HOUR);
        list.add(kIndexPopBean9);
        KIndexPopBean kIndexPopBean10 = new KIndexPopBean(false, ResStringUtil.getString(R.string.day_k_3), K_LINE_TYPE_3_DAY);
        list.add(kIndexPopBean10);
        KIndexPopBean kIndexPopBean11 = new KIndexPopBean(false, ResStringUtil.getString(R.string.week_k_), K_LINE_TYPE_WEEK);
        list.add(kIndexPopBean11);
        KIndexPopBean kIndexPopBean12 = new KIndexPopBean(false, ResStringUtil.getString(R.string.month_k_), K_LINE_TYPE_MONTH);
        list.add(kIndexPopBean12);
        for (KIndexPopBean kIndexPopBean : list) {
            if (currentSelectedIndex.equals(kIndexPopBean.getName())) {
                kIndexPopBean.setSelected(true);
            }
        }
        return list;
    }

    public static void setKIndexTabSelected(Context context, TabLayout tabLayout){
        View tabItemView = tabLayout.getTabAt(4).getCustomView();
        if (tabItemView == null) {
            return;
        }
        ((TextView)tabItemView.findViewById(R.id.tv_K_index))
                .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_bottom_triangle)).setColorFilter(context.getResources().getColor(R.color.chart_accent_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_top_triangle)).setColorFilter(context.getResources().getColor(R.color.chart_accent_text_color));
        tabItemView.findViewById(R.id.iv_right_bottom_triangle).setVisibility(View.VISIBLE);
        tabItemView.findViewById(R.id.iv_right_top_triangle).setVisibility(View.INVISIBLE);
        tabLayout.getTabAt(4).setCustomView(tabItemView);
    }

    public static void setKIndexTabUnSelected(Context context, TabLayout tabLayout) {
        View tabItemView = tabLayout.getTabAt(4).getCustomView();
        if (tabItemView == null) {
            return;
        }
        ((TextView)tabItemView.findViewById(R.id.tv_K_index))
                .setTextColor(context.getResources().getColor(R.color.main_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_bottom_triangle)).setColorFilter(context.getResources().getColor(R.color.main_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_top_triangle)).setColorFilter(context.getResources().getColor(R.color.main_text_color));
        tabItemView.findViewById(R.id.iv_right_bottom_triangle).setVisibility(View.VISIBLE);
        tabItemView.findViewById(R.id.iv_right_top_triangle).setVisibility(View.INVISIBLE);
        tabLayout.getTabAt(4).setCustomView(tabItemView);
    }

    public static void setKIndexTabShowPop(Context context, TabLayout tabLayout) {
        View tabItemView = tabLayout.getTabAt(4).getCustomView();
        if (tabItemView == null) {
            return;
        }
        ((TextView)tabItemView.findViewById(R.id.tv_K_index))
                .setTextColor(context.getResources().getColor(R.color.main_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_bottom_triangle)).setColorFilter(context.getResources().getColor(R.color.main_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_top_triangle)).setColorFilter(context.getResources().getColor(R.color.main_text_color));
        tabItemView.findViewById(R.id.iv_right_bottom_triangle).setVisibility(View.INVISIBLE);
        tabItemView.findViewById(R.id.iv_right_top_triangle).setVisibility(View.VISIBLE);
        tabLayout.getTabAt(4).setCustomView(tabItemView);
    }

    public static void setTabIndexText(Context context, TabLayout tabLayout, String index) {
        View tabItemView = tabLayout.getTabAt(4).getCustomView();
        if (tabItemView == null) {
            return;
        }
        ((TextView)tabItemView.findViewById(R.id.tv_K_index)).setText(index);
        ((TextView)tabItemView.findViewById(R.id.tv_K_index))
                .setTextColor(context.getResources().getColor(R.color.chart_accent_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_bottom_triangle)).setColorFilter(context.getResources().getColor(R.color.chart_accent_text_color));
        ((ImageView) tabItemView.findViewById(R.id.iv_right_top_triangle)).setColorFilter(context.getResources().getColor(R.color.chart_accent_text_color));
        tabItemView.findViewById(R.id.iv_right_bottom_triangle).setVisibility(View.VISIBLE);
        tabItemView.findViewById(R.id.iv_right_top_triangle).setVisibility(View.INVISIBLE);
        tabLayout.getTabAt(4).setCustomView(tabItemView);
    }
}
